package com.roads.project.service.impl;

import com.roads.common.constant.Constants;
import com.roads.common.utils.StringUtils;
import com.roads.project.domain.ProInstitution;
import com.roads.project.domain.bid.Bid;
import com.roads.project.domain.bid.BidSelect;
import com.roads.project.mapper.ProInstitutionMapper;
import com.roads.project.service.IBidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BidServiceImpl
 * @Description 标段service实现类
 * @Author nhp
 * @Date 2021/11/30 0030 14:55
 * @Version 1.0.0
 **/
@Service
public class BidServiceImpl implements IBidService {

    @Autowired
    private ProInstitutionMapper institutionMapper;

    /**
     * group分组下拉框 由项目和机构组成
     * 根据当前登录用户获取标段和分部
     * 如果标段共享清单则只显示标段
     * @param userName 当前登录用户
     * @param isAdmin 是否管理员
     * @return 标段按照项目分组select下拉框
     */
    @Override
    public List<BidSelect> selectProjectBidOrBranch(String userName, boolean isAdmin) {
        List<BidSelect> list = new ArrayList<>();
        List<ProInstitution> institutions;
        ProInstitution search = new ProInstitution();
        if (!isAdmin) {
            search.setCreateBy(userName);
        }
        // 查询出标段和分包
        institutions = institutionMapper.selectProjectBidBranch(search);
        // 临时集合
        Map<Integer, String> projectMap = new HashMap<>();
        Map<String, ProInstitution> institutionMap = new HashMap<>();
        if (institutions.size() > 0) {
            for (ProInstitution institution : institutions) {
                String mapValue = projectMap.get(institution.getProjectId());
                if (StringUtils.isEmpty(mapValue)) {
                    // 创建项目
                    BidSelect bidSelect = new BidSelect();
                    bidSelect.setName(institution.getProjectName());
                    list.add(bidSelect);
                    projectMap.put(institution.getProjectId(), institution.getProjectName());
                }
                // 标段处理，不论标段有无分部都
                if (Constants.INSTITUTION_TYPE_B.equals(institution.getInstitutionType())) {
                    createBid(institution, list);
                } else {
                    ProInstitution parent = institutionMapper.getModelByInnerCode(institution.getParentCode());
                    boolean isShare = Constants.YES.equals(parent.getIsShare());
                    if (!isShare) {
                        createBid(institution, list);
                    } else {
                        ProInstitution parentTemp = institutionMap.get(parent.getInnerCode());
                        if (StringUtils.isNull(parentTemp)) {
                            createBid(parent, list);
                            institutionMap.put(parent.getInnerCode(), parent);
                        }
                    }
                }
                institutionMap.put(institution.getInnerCode(), institution);
            }
        }
        return list;
    }

    /**
     * group分组下拉框 由项目和机构组成
     * 根据当前登录用户获取标段和分部 所有标段和分部
     * @param userName 当前登录用户
     * @param isAdmin 是否管理员
     * @return 标段按照项目分组select下拉框
     */
    @Override
    public List<BidSelect> selectProjectBidBranch(String userName, boolean isAdmin) {
        List<BidSelect> list = new ArrayList<>();
        List<ProInstitution> institutions;
        ProInstitution search = new ProInstitution();
        if (!isAdmin) {
            search.setCreateBy(userName);
        }
        // 查询出标段和分包
        institutions = institutionMapper.selectProjectBidBranch(search);
        // 临时集合
        Map<Integer, String> projectMap = new HashMap<>();
        Map<String, ProInstitution> institutionMap = new HashMap<>();
        if (institutions.size() > 0) {
            for (ProInstitution institution : institutions) {
                String mapValue = projectMap.get(institution.getProjectId());
                if (StringUtils.isEmpty(mapValue)) {
                    // 创建项目
                    BidSelect bidSelect = new BidSelect();
                    bidSelect.setName(institution.getProjectName());
                    list.add(bidSelect);
                    projectMap.put(institution.getProjectId(), institution.getProjectName());
                }
                createBid(institution, list);
                institutionMap.put(institution.getInnerCode(), institution);
            }
        }
        return list;
    }

    /**
     * 普通下拉框
     * @param userName 当前登录用户
     * @param isAdmin 是否admin
     * @return 机构集合
     */
    @Override
    public List<ProInstitution> selectBid(String userName, boolean isAdmin) {

        return null;
    }

    /**
     * group分组下拉框 由项目和机构组成
     * 根据当前登录用户获取标段 所有标段
     * @param userName 当前登录用户
     * @param isAdmin 是否管理员
     * @return 标段按照项目分组select下拉框
     */
    @Override
    public List<BidSelect> selectProjectBid(String userName, boolean isAdmin) {
        List<BidSelect> list = new ArrayList<>();
        List<ProInstitution> institutions;
        ProInstitution search = new ProInstitution();
        if (!isAdmin) {
            search.setCreateBy(userName);
        }
        // 查询出标段和分包
        institutions = institutionMapper.selectProjectBid(search);
        // 临时集合
        Map<Integer, String> projectMap = new HashMap<>();
        Map<String, ProInstitution> institutionMap = new HashMap<>();
        if (institutions.size() > 0) {
            for (ProInstitution institution : institutions) {
                String mapValue = projectMap.get(institution.getProjectId());
                if (StringUtils.isEmpty(mapValue)) {
                    // 创建项目
                    BidSelect bidSelect = new BidSelect();
                    bidSelect.setName(institution.getProjectName());
                    list.add(bidSelect);
                    projectMap.put(institution.getProjectId(), institution.getProjectName());
                }
                createBid(institution, list);
                institutionMap.put(institution.getInnerCode(), institution);
            }
        }
        return list;
    }

    private void createBid(ProInstitution institution, List<BidSelect> list) {
        String projectName = institution.getProjectName();
        for (BidSelect bidSelect : list) {
            if (projectName.equals(bidSelect.getName())) {
                List<Bid> bids = bidSelect.getBids();
                if (StringUtils.isNull(bids)) {
                    bids = new ArrayList<>();
                    bidSelect.setBids(bids);
                }
                Bid bid = new Bid();
                bid.setCode(institution.getInnerCode());
                bid.setName(institution.getInstitutionName());
                bids.add(bid);
            }
        }
    }

    /**
     * 普通下拉框 由机构组成
     * 根据当前登录用户获取所有机构（F,B类型机构）
     * @param userName 当前登录用户
     * @param isAdmin 是否管理员
     * @return 机构集合
     */
    @Override
    public List<ProInstitution> selectBidBranch(String userName, boolean isAdmin) {
        ProInstitution search = new ProInstitution();
        if (!isAdmin) {
            search.setCreateBy(userName);
        }
        List<ProInstitution> institutions = institutionMapper.selectBidBranch(search);;
        return institutions;
    }

    /**
     * 根据项目ID获取所有结构
     *
     * @param search 查询条件
     * @return 机构集合
     */
    @Override
    public List<ProInstitution> selectBidByProjectId(ProInstitution search) {
        return institutionMapper.selectBidByProjectId(search);
    }

    /**
     * 根据项目ID获取所有标段和分部
     *
     * @param projectId 项目ID
     * @return 标段分部集合
     */
    @Override
    public List<ProInstitution> selectBidAndBranchByProjectId(Integer projectId) {
        return institutionMapper.selectBidAndBranchByProjectId(projectId);
    }

    /**
     * 普通下拉框--汇总功能使用
     * 根据当前登录用户获取所有机构（F,B类型机构）
     * @param userName 当前登录用户
     * @return 机构集合
     */
    @Override
    public List<ProInstitution> selectSummaryBidBranch(String userName) {
        ProInstitution search = new ProInstitution();
        search.setCreateBy(userName);
        List<ProInstitution> institutions = institutionMapper.selectBidBranch(search);;
        return institutions;
    }

    /**
     * 监理标段group分组下拉框 由项目和机构组成
     * 根据当前登录用户获取监理标段
     * @param userName 当前登录用户
     * @param isAdmin 是否管理员
     * @param reportType 报表类型 3 是驻地、监理 4 是总监
     * @return 监理标段按照项目分组select下拉框
     */
    @Override
    public List<BidSelect> selectProjectSupervisorBid(String userName, boolean isAdmin, String reportType) {
        List<BidSelect> list = new ArrayList<>();
        List<ProInstitution> institutions;
        ProInstitution search = new ProInstitution();
        if (!isAdmin) {
            search.setCreateBy(userName);
        }
        search.setInstitutionType(reportType);
        // 查询出标段和分包
        institutions = institutionMapper.selectProjectSupervisorBid(search);
        // 临时集合
        Map<Integer, String> projectMap = new HashMap<>();
        Map<String, ProInstitution> institutionMap = new HashMap<>();
        if (institutions.size() > 0) {
            for (ProInstitution institution : institutions) {
                String mapValue = projectMap.get(institution.getProjectId());
                if (StringUtils.isEmpty(mapValue)) {
                    // 创建项目
                    BidSelect bidSelect = new BidSelect();
                    bidSelect.setName(institution.getProjectName());
                    list.add(bidSelect);
                    projectMap.put(institution.getProjectId(), institution.getProjectName());
                }
                createBid(institution, list);
                institutionMap.put(institution.getInnerCode(), institution);
            }
        }
        return list;
    }

    /**
     * 监理普通下拉框--主要用于监理计量新增
     *
     * @param userName 当前登录用户
     * @return 机构集合
     */
    @Override
    public List<ProInstitution> selectSupervisorBid(String userName) {
        ProInstitution search = new ProInstitution();
        search.setMeterageUser(userName);
        List<ProInstitution> institutions = institutionMapper.selectSupervisorBid(search);;
        return institutions;
    }

    /**
     * 代建普通下拉框--主要用于代建计量新增
     *
     * @param userName 当前登录用户
     * @return 机构集合
     */
    @Override
    public List<ProInstitution> selectAgencyBid(String userName) {
        ProInstitution search = new ProInstitution();
        search.setMeterageUser(userName);
        List<ProInstitution> institutions = institutionMapper.selectAgencyBid(search);;
        return institutions;
    }

    /**
     * 监理普通下拉框--主要用于监理汇总
     *
     * @param userName 当前登录用户
     * @return 机构集合
     */
    @Override
    public List<ProInstitution> selectSummarySupervisorBid(String userName) {
        ProInstitution search = new ProInstitution();
        search.setMeterageUser(userName);
        List<ProInstitution> institutions = institutionMapper.selectSummarySupervisorBid(search);;
        return institutions;
    }

    /**
     * 代建普通下拉框--主要用于代建汇总
     *
     * @param userName 当前登录用户
     * @return 机构集合
     */
    @Override
    public List<ProInstitution> selectSummaryAgencyBid(String userName) {
        ProInstitution search = new ProInstitution();
        search.setMeterageUser(userName);
        List<ProInstitution> institutions = institutionMapper.selectSummaryAgencyBid(search);;
        return institutions;
    }

}
